// ActionScript file
	import mx.controls.dataGridClasses.DataGridColumn;
			import mx.controls.dataGridClasses.DataGridBase;
			import mx.collections.ArrayCollection;
			import mx.utils.ObjectUtil;
			import com.adobe.serialization.json.JSON;
			import mx.controls.List;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
		
			[Bindable]
			public var methods:Object;
			[Bindable]
			public var currentService:Object;
			[Bindable]
			public var currentMethod:Object;
			[Bindable]
			public var currentMethodName:String;
			
			var sro:RemoteObject;
			
			var time:Number;
			
			function setEndpoint()
			{
				var url:String = Application.application.url;
				if(url.indexOf('http') != -1)
				{
					ro.endpoint = url.substr(0, url.lastIndexOf('/')) +  "gateway.php";
				}
			}
			
			function init():void
			{
				sro = new RemoteObject("amfphp");
				sro.addEventListener(ResultEvent.RESULT, onResult);
				sro.addEventListener(FaultEvent.FAULT, onFault);
			}
			
			function describeService(data:Object):void
			{
				methods = data;
				var links:Array = new Array();
				for(var i in data)
				{
					links.push({label:i, data:i});
				}
				links = links.sortOn("label");
				if(links.length > 0)
				{
					methodLinks.dataProvider = links;
					methodLinks.selectedIndex = 0;
					currentMethod = methods[links[0].data];
					currentMethodName = links[0].data;
					currentState = "testService";
				}
			}
			
			function sendRequest()
			{
				time = getTimer();
				currentMethodName = methodLinks.dataProvider[methodLinks.selectedIndex].data;
				sro.source = currentService.data.split('/').join('.').split('\\').join('.') + currentService.label;
				sro[currentMethodName].send.apply(null, getArgs());	
			}
			
			function getArgs()
			{
				var parsedArgs:Array = new Array();
				if(arg is Array)
				{
					for(var i:int = 0; i < arg.length; i++)
					{
						try
						{
							var text = arg[i].text;
							if(text.indexOf('{') == -1 && 
							   text.indexOf('[') == -1 &&
							   text.indexOf('"') == -1 &&
							   text.indexOf("'") == -1)
							{
								var val = parseInt(text);
								if(isNaN(val))
								{
									text = '"' + text + '"';
								}
							}
							parsedArgs.push(JSON.decode(text));
						}
						catch(e:*)
						{
							
						}
					}
				}
				return parsedArgs;
			}
			
			function onResult(event)
			{
				currentState = "testMethod";
				resultsText.text = ObjectUtil.toString(event.result);
				infoText.text = "Query took: " + (getTimer() - time) + " ms";
				
				if((event.result is Array && event.result[0] is Object) ||
				   (event.result is ArrayCollection && event.result.getItemAt(0) is Object))
				{
					var keys = new Array();
					for(var i in event.result[0])
					{
						keys.push(new DataGridColumn(i));
					}
					resultsDg.columns = keys;
					resultsDg.dataProvider = event.result;
				}
				else
				{
					resultsDg.columns = new Array();
					resultsDg.dataProvider = new Array();
				}
				output.selectedIndex = 0;
			}
			
			function onFault(event)
			{
				currentState = "testMethod";
				resultsText.text = ObjectUtil.toString(event.fault);
				infoText.text = "Query took: " + (getTimer() - time) + " ms";
				resultsDg.dataProvider = new Array();
				output.selectedIndex = 0;
			}
			
			function enableLinks()
			{
			  for(var i=0; i<methodLinks.numChildren; i++){
			   var curItem = methodLinks.getChildAt(i);
			   curItem.enabled = i == methodLinks.selectedIndex ? false : true;
			  }
			}
